package com.linkage.educloud.ah.view.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.linkage.educloud.ah.R;
import com.linkage.educloud.ah.view.BasicFragment;
import com.linkage.educloud.ah.widget.MyGuideGallery;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * <desc>
 * Created by The Moss on 2018/8/7.
 */

public class GuideFragment extends BasicFragment {
    @BindView(R.id.guide_gallery)
    MyGuideGallery mGallery;
    @BindView(R.id.guide_point_linear)
    LinearLayout mPointLinear;
    @BindView(R.id.guide_startBtn)
    Button mStartBtn;

    private int positon = 0;
    private static int[] GUIDES_IMAGE = {R.mipmap.guide1, R.mipmap.guide2,
            R.mipmap.guide3};

    public static GuideFragment newInstance() {
        GuideFragment fragment = new GuideFragment();
//        fragment.setTargetFragment(splashFragment,0);
        return fragment;
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.fragment_guide;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
    }

    private void initView() {
        mGallery.setAdapter(new ImageAdapter());
        mPointLinear.setBackgroundColor(Color.argb(0, 135, 135, 152));
        for (int i = 0; i < GUIDES_IMAGE.length; i++) {
            ImageView pointView = new ImageView(getActivity());
            if (i == 0) {
                pointView.setBackgroundResource(R.mipmap.feature_point_cur);
            } else
                pointView.setBackgroundResource(R.mipmap.feature_point);
            mPointLinear.addView(pointView);
        }
        mGallery.SetOnChangeListener(new MyGuideGallery.GuideChangeListener() {

            @Override
            public void OnViewChange(int keyCode, int postion, int totalN) {
                if (postion == totalN - 1) {
                    if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
                        doFinish();
                    }
                }
            }

            @Override
            public void doWhenFinish() {
                // TODO Auto-generated method stub

            }

        });
    }

    private void changePointView(int cur) {
        View view = mPointLinear.getChildAt(positon);
        View curView = mPointLinear.getChildAt(cur);
        if (view != null && curView != null) {
            ImageView pointView = (ImageView) view;
            ImageView curPointView = (ImageView) curView;
            pointView.setBackgroundResource(R.mipmap.feature_point);
            curPointView.setBackgroundResource(R.mipmap.feature_point_cur);
            positon = cur;
        }
    }

    private class ImageAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return GUIDES_IMAGE.length;
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return GUIDES_IMAGE[position];
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView = new ImageView(getActivity());
            imageView.setImageResource(GUIDES_IMAGE[position]);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setLayoutParams(new Gallery.LayoutParams(
                    Gallery.LayoutParams.FILL_PARENT, Gallery.LayoutParams.FILL_PARENT));
            mHandler.sendEmptyMessage(position);
            return imageView;
        }

    }

    Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            changePointView(msg.what);
        }

    };

    @OnClick(R.id.guide_startBtn)
    public void onClick() {
        doFinish();
    }

    private void doFinish() {
//        getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, new Intent());
//        fragmentManager.popBackStack(GuideFragment.class.getSimpleName(), FragmentManager.POP_BACK_STACK_INCLUSIVE);
        fragmentTransaction.replace(R.id.fragment_content, SplashFragment.newInstance(), "splash")
                .commit();
    }
    @Override
    public boolean onBackPressed() {
        getActivity().finish();
        return true;
    }
}
